package com.bodacn.storagescan;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.format.DateFormat;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.UUID;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

public class CarRecEditActivity extends AppCompatActivity implements View.OnClickListener {
    private String TAG=this.getClass().getName();
    public static final int MSG_DetailOver = 1001;
    public static final int MSG_Material =1002;
    public static final int MSG_ERROR = 9009;
    ImageButton btn_return;
    EditText edt_StartDate,edt_EndDate;
    TextView txt_titleInv;
    ListView detailList;
    String curLoginUserNo,curLoginUser,FormType,CustName,curLoginTime;//操作员所属部门等信息;
    JSONArray detailJSON,searchJSON,headJSON;
    EditText edt_MaterialName,edt_Model,edt_DoorWidth,
            edt_BowlNo,edt_SquareWeight,edt_Number,edt_Uint,
            edt_NumberAssist,edt_UintAssist,edt_StyleNo,edt_Diameter;
    View contentView;
    LinearLayout templetlisthead;
    TextView tv_pop_title;
    EditText edt_pop_fliter;
    PopupWindow popupWindow;
    LinearLayout mainLinerLayout;
    ListView lv_Detail;
    String selPurchaseNo ="",selSupplierName="";

    //全局变量
    Toast curToast;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_carrec_edit);
        InitView();
        //获取登陆参数
        Bundle paramBundle = this.getIntent().getExtras();
        curLoginUserNo=paramBundle.getString("LoginUserNo");
        curLoginUser=paramBundle.getString("LoginUser");
        curLoginTime=paramBundle.getString("LoginTime");
        CustName=paramBundle.getString("CustName");
        FormType=paramBundle.getString("FormType","UNKNOW");
        selPurchaseNo=paramBundle.getString("PurchaseNo","UNKNOW");
        selSupplierName=paramBundle.getString("SupplierName","UNKNOW");
        System.out.println("selSupplierName:"+selSupplierName);
        ((TextView)findViewById(R.id.tv_userName)).setText(curLoginUser);
        //初始提醒;
        curToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
        //先显示对话框,再调用查询加载
        initPopwindow();
    }

    private void InitView(){
        edt_StyleNo=findViewById(R.id.edt_StyleNo);
        edt_MaterialName=findViewById(R.id.edt_MaterialName);
        edt_MaterialName.setOnClickListener(this);
        edt_Model=findViewById(R.id.edt_Model);
        edt_DoorWidth=findViewById(R.id.edt_DoorWidth);
        edt_BowlNo=findViewById(R.id.edt_BowlNo);
        edt_SquareWeight=findViewById(R.id.edt_SquareWeight);
        edt_Number=findViewById(R.id.edt_Number);
        edt_Uint=findViewById(R.id.edt_Uint);
        edt_NumberAssist=findViewById(R.id.edt_NumberAssist);
        edt_Diameter=findViewById(R.id.edt_Diameter);
        edt_UintAssist=findViewById(R.id.edt_UintAssist);
        btn_return=findViewById(R.id.btn_return);
        btn_return.setOnClickListener(this);
        txt_titleInv=findViewById(R.id.txt_titleInv);
        detailList=findViewById(R.id.detailList);
        findViewById(R.id.btn_Clear).setOnClickListener(this);
        findViewById(R.id.btn_Affirm).setOnClickListener(this);
        mainLinerLayout=findViewById(R.id.mainLinerLayout);

    }



    private void initPopwindow() {
        contentView = LayoutInflater.from((Context)this).inflate(R.layout.popselect, null);
        popupWindow = new PopupWindow(contentView, -1, -2);
        popupWindow.setFocusable(true);
        contentView.findViewById(R.id.btn_pop_cancel).setOnClickListener(new View.OnClickListener() {
            public void onClick(View param1View) {
                if (popupWindow != null && popupWindow.isShowing())
                    popupWindow.dismiss();
            }
        });
        edt_pop_fliter =contentView.findViewById(R.id.edt_pop_fliter);
        tv_pop_title = (TextView)contentView.findViewById(R.id.tv_pop_title);
        lv_Detail = (ListView)contentView.findViewById(R.id.lv_Detail);
        templetlisthead = (LinearLayout)contentView.findViewById(R.id.templetlisthead);
        popupWindow.setBackgroundDrawable((Drawable)new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
    }
    private void showSelectMaterialName(){
        try {
            searchJSON=new JSONArray("[]");
            headJSON = new JSONArray("[{\"TitleName\":\"StyleNo\",\"TitleText\":\"款号\",\"TxtSize\":20,\"TxtWidth\":150,\"AutoZoom\":1,\"Align\":0,\"Color\":\"Main\"},"
                    +"{\"TitleName\":\"MaterialName\",\"TitleText\":\"物料名称\",\"TxtSize\":20,\"TxtWidth\":250,\"AutoZoom\":1,\"Align\":0,\"Color\":\"Main\"},"
                    +"{\"TitleName\":\"Model\",\"TitleText\":\"规格型号\",\"TxtSize\":20,\"TxtWidth\":250,\"AutoZoom\":1,\"Align\":0,\"Color\":\"Main\"},"
                    +"{\"TitleName\":\"DoorWidth\",\"TitleText\":\"门幅\",\"TxtSize\":20,\"TxtWidth\":120,\"AutoZoom\":1,\"Align\":0,\"Color\":\"Main\"},"
                    +"{\"TitleName\":\"SquareWeight\",\"TitleText\":\"平方克重\",\"TxtSize\":20,\"TxtWidth\":150,\"AutoZoom\":1,\"Align\":0,\"Color\":\"Main\"},"
                    +"{\"TitleName\":\"Uint\",\"TitleText\":\"单位\",\"TxtSize\":20,\"TxtWidth\":100,\"AutoZoom\":1,\"Align\":0,\"Color\":\"Main\"},"
                    +"{\"TitleName\":\"UintAssist\",\"TitleText\":\"辅助单位\",\"TxtSize\":20,\"TxtWidth\":150,\"AutoZoom\":1,\"Align\":0,\"Color\":\"Main\"}"
                    +"]");
        } catch (JSONException jSONException) {
            jSONException.printStackTrace();
        }
        tv_pop_title.setText("供应商选择");
        contentView.findViewById(R.id.btn_pop_ok).setOnClickListener(new View.OnClickListener() {
            public void onClick(View param1View) {
                if (selSupplierName.length() > 0){
                    if (popupWindow != null && popupWindow.isShowing()) popupWindow.dismiss();
                } else {
                    curToast.setText("请选择需要选择的内容！");
                    curToast.show();
                }
            }
        });
        contentView.findViewById(R.id.linear_pop_search).setVisibility(View.VISIBLE);
        contentView.findViewById(R.id.btn_pop_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SearchSelectMaterial(edt_pop_fliter.getText().toString());
                hideIM(v);
            }
        });
        CreateListHead();
        lv_Detail.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> param1AdapterView, View view, int postion, long param1Long) {
                try {
                    view.setSelected(true);
                    if (searchJSON.optJSONObject(postion).has("StyleNo") == true)  edt_StyleNo.setText(searchJSON.optJSONObject(postion).getString("StyleNo"));
                    if (searchJSON.optJSONObject(postion).has("MaterialName") == true)  edt_MaterialName.setText(searchJSON.optJSONObject(postion).getString("MaterialName"));
                    if (searchJSON.optJSONObject(postion).has("Model") == true)  edt_Model.setText(searchJSON.optJSONObject(postion).getString("Model"));
                    if (searchJSON.optJSONObject(postion).has("DoorWidth") == true)  edt_DoorWidth.setText(searchJSON.optJSONObject(postion).getString("DoorWidth"));
                    if (searchJSON.optJSONObject(postion).has("BowlNo") == true)  edt_BowlNo.setText(searchJSON.optJSONObject(postion).getString("BowlNo"));
                    if (searchJSON.optJSONObject(postion).has("SquareWeight") == true)  edt_SquareWeight.setText(searchJSON.optJSONObject(postion).getString("SquareWeight"));
                    if (searchJSON.optJSONObject(postion).has("Number") == true)  edt_Number.setText(searchJSON.optJSONObject(postion).getString("Number"));
                    if (searchJSON.optJSONObject(postion).has("Uint") == true)  edt_Uint.setText(searchJSON.optJSONObject(postion).getString("Uint"));
                    if (searchJSON.optJSONObject(postion).has("NumberAssist") == true)  edt_NumberAssist.setText(searchJSON.optJSONObject(postion).getString("NumberAssist"));
                    if (searchJSON.optJSONObject(postion).has("UintAssist") == true)  edt_UintAssist.setText(searchJSON.optJSONObject(postion).getString("UintAssist"));

                } catch (JSONException jSONException) {
                    jSONException.printStackTrace();
                }
            }
        });
        //控制屏幕宽度
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        System.out.println("屏宽:" + dm.widthPixels);
        System.out.println("屏高:" + dm.heightPixels);
        popupWindow.setWidth(dm.widthPixels);
        popupWindow.setHeight(dm.heightPixels-120);
        popupWindow.showAtLocation(mainLinerLayout, Gravity.BOTTOM,0,0);
        //加载默认的历史合约;
        SearchSelectMaterial("");
    }

    //dp 转换成px
    public int dp2px(float dp)
    {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }

    //过滤订单
    private void SearchSelectMaterial(String paramMaterial) {
        //执行SQL过程
        String SqlComm="Exec App_SelectGetData @OperType='Material',@SupplierName='" +selSupplierName+"',@PurchaseNo='"+selPurchaseNo
                +"',@Material='"+paramMaterial+"',@UserNo='"+curLoginUserNo+"'";
        //调用WebService过程并处理界面更新;
        ReadSQLData(SqlComm);

    }


    //创建列;
    private void CreateListHead() {
        System.out.println("CreateListHead()");
        //内容为表头，宽度，自适应;
        if ((headJSON != null) && (headJSON.length() > 0)) {
            int childCount = templetlisthead.getChildCount();
            for (int i = childCount - 1; i >= 0; i--) {
                View view = templetlisthead.getChildAt(i);
                if (view instanceof TextView) {
                    templetlisthead.removeViewAt(i);
                }
            }
            //创建列
            String TitleText,TitleName;
            float  txtWidth;
            int TxtSize,AutoZoom,Align;
            for (int i=0;i<headJSON.length();i++)
                if (headJSON.optJSONObject(i).has("TitleText") == true) {
                    try {
                        System.out.println("CreateListHead()"+i);
                        TitleName = headJSON.optJSONObject(i).getString("TitleName");
                        TitleText = headJSON.optJSONObject(i).getString("TitleText");
                        TxtSize = headJSON.optJSONObject(i).getInt("TxtSize");
                        txtWidth = headJSON.optJSONObject(i).getLong("TxtWidth");
                        AutoZoom = headJSON.optJSONObject(i).getInt("AutoZoom");
                        TextView tv = new TextView(this);
                        tv.setText(TitleText);
                        tv.setWidth((int) txtWidth);
                        tv.setTextColor(getResources().getColor(R.color.white));
                        tv.setTextSize(TxtSize);
                        if (headJSON.optJSONObject(i).has("Align")==true) {
                            Align = headJSON.optJSONObject(i).getInt("Align");
                            switch (Align) {
                                case 0:
                                    tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                                    break;
                                case 1:
                                    tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                                    break;
                                case 2:
                                    tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
                                    break;
                                default:
                                    tv.setGravity(Gravity.CENTER_VERTICAL);
                                    break;
                            }
                        }
                        //自定义文字颜色;
                        if (headJSON.optJSONObject(i).has("Color")==true){
                            tv.setTextColor(GetColorValue( headJSON.optJSONObject(i).getString("Color")));
                        }
                        //添加到View中;
                        if (AutoZoom==1) templetlisthead.addView(tv,new LinearLayout.LayoutParams((int) txtWidth, MATCH_PARENT, 1));
                        else templetlisthead.addView(tv);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            templetlisthead.postInvalidate();
        }
    }

    //判断是否为数字，不然不转
    public static boolean isNumeric(String str){
        try {
            new BigDecimal(str);
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    //从字符串转颜色
    private int GetColorValue(String paramColor){
        int PenColor= Color.WHITE;
        String StrPenColor=paramColor;
        if (StrPenColor.indexOf("#")==0){
            PenColor=Color.parseColor(StrPenColor);
        }else {
            if (isNumeric(StrPenColor)) {
                int testColor = Integer.parseInt(StrPenColor);
                int red = (testColor & 0xff0000) >> 16;
                int green = (testColor & 0x00ff00) >> 8;
                int blue = (testColor & 0x0000ff);
                PenColor = Color.rgb(red, green, blue);
            } else {
                if (StrPenColor.equals("Black") == true)
                    PenColor = Color.BLACK;
                if (StrPenColor.equals("Blue") == true)
                    PenColor = Color.BLUE;
                if (StrPenColor.equals("Yellow") == true)
                    PenColor = Color.YELLOW;
                if (StrPenColor.equals("Red") == true)
                    PenColor = Color.RED;
                if (StrPenColor.equals("Green") == true)
                    PenColor = Color.GREEN;
                if (StrPenColor.equals("Gray") == true)
                    PenColor = Color.LTGRAY;
                if (StrPenColor.equals("White") == true)
                    PenColor = Color.WHITE;
                if (StrPenColor.equals("Main") == true)
                    PenColor = getResources().getColor(R.color.text_main);
            }
        }
        return PenColor;
    }

    //数据适配器，继承自BaseAdapter;
    private final class SelectTempletListAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        public SelectTempletListAdapter(Context context) {
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        public  String TrnasValue(String params)
        {
            if ((params.toUpperCase()=="NULL".toUpperCase())||(params=="0")||(params=="0.0"))
                return "";
            else return params;
        }
        @Override
        public int getCount() {
            //每页行数
            return searchJSON.length();
        }
        @Override
        public Object getItem(int position) {
            try {
                if (position<searchJSON.length())
                    return searchJSON.getJSONArray(position);
                else
                    return null;
            } catch (JSONException e)
            {
                e.printStackTrace();
                System.out.println("getItem取JSON数据出错!");
                return  null;
            }
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = inflater.inflate(R.layout.popselect_item, null, false);
            int pullVatListWidth=parent.getWidth();
            view.setLayoutParams(new ListView.LayoutParams((int)pullVatListWidth,dp2px(45)));
            //转换分页码;
            int curPosition= position;
            String tmpValue;
            int color = getResources().getColor(R.color.text_black);
            if (curPosition<searchJSON.length()) {
                try {
                    Log.d(TAG, "getView()----------------------------------------------------->" + position);
                    if (searchJSON.optJSONObject(curPosition).has("ConColor") == true) {
                        tmpValue = searchJSON.optJSONObject(curPosition).getString("ConColor");
                        color=GetColorValue(tmpValue);
                    }
                    for (int i = 0; i < headJSON.length(); i++)
                        if (headJSON.optJSONObject(i).has("TitleText") == true) {
                            try {
                                String TitleName = headJSON.optJSONObject(i).getString("TitleName");
                                if (searchJSON.optJSONObject(curPosition).has(TitleName)==true) tmpValue= searchJSON.optJSONObject(curPosition).getString(TitleName);
                                else tmpValue="unReturn";
                                int TxtSize = headJSON.optJSONObject(i).getInt("TxtSize");
                                float txtWidth = headJSON.optJSONObject(i).getLong("TxtWidth");
                                int AutoZoom = headJSON.optJSONObject(i).getInt("AutoZoom");
                                TextView tv = new TextView(view.getContext());
                                tv.setText(TrnasValue(tmpValue));
                                tv.setWidth((int) txtWidth);
                                tv.setTextColor(color);
                                tv.setGravity(Gravity.CENTER_VERTICAL);
                                if (headJSON.optJSONObject(i).has("Align")==true) {
                                    int Align;
                                    Align = headJSON.optJSONObject(i).getInt("Align");
                                    switch (Align) {
                                        case 0:
                                            tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                                            break;
                                        case 1:
                                            tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                                            break;
                                        case 2:
                                            tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
                                            break;
                                        default:
                                            tv.setGravity(Gravity.CENTER_VERTICAL);
                                            break;
                                    }
                                }
                                tv.setTextSize(TxtSize);
                                //添加到View中;
                                if (AutoZoom == 1)
                                    ((LinearLayout)view).addView(tv, new LinearLayout.LayoutParams((int) txtWidth, MATCH_PARENT, 1));
                                else ((LinearLayout)view).addView(tv);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
            return view;
        }
    }

    /*
     * 定义一个Handler用于接收Fragment发出来的指令
     */
    public Handler fragmentHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg!=null){
                String tmpValue;
                //任意消息来均置焦点
                int msgId = msg.what;
                switch (msgId) {
                    case MSG_Material:
                        SelectTempletListAdapter selectTempletListAdapter = new SelectTempletListAdapter(contentView.getContext());
                        lv_Detail.setAdapter(selectTempletListAdapter);
                        break;
                    default:
                        break;
                }
            }
        }
    };

    @Override
    public void onClick(View view) {
        hideIM(view);
        switch (view.getId()){
            case R.id.btn_Clear:
                ClearScreen();
                break;
            case R.id.edt_MaterialName:
                showSelectMaterialName();
                break;
            case R.id.btn_Affirm:
                if (edt_MaterialName.getText().toString().length()>0
                        &&edt_Number.getText().length()>0&&
                        edt_NumberAssist.getText().length()>0)
                {
                    try {
                        JSONObject jsonObject=new JSONObject();
                        jsonObject.put("styleNo",edt_StyleNo.getText().toString());
                        jsonObject.put("materialName",edt_MaterialName.getText().toString());
                        jsonObject.put("model",edt_Model.getText().toString());
                        jsonObject.put("doorWidth",edt_DoorWidth.getText().toString());
                        jsonObject.put("bowlNo",edt_BowlNo.getText().toString());
                        jsonObject.put("squareWeight",edt_SquareWeight.getText().toString());
                        jsonObject.put("number",edt_Number.getText().toString());
                        jsonObject.put("uint",edt_Uint.getText().toString());
                        jsonObject.put("numberAssist",edt_NumberAssist.getText().toString());
                        jsonObject.put("uintAssist",edt_UintAssist.getText().toString());
                        jsonObject.put("diameter",edt_Diameter.getText().toString());
                        //返回
                        Intent intent = new Intent();
                        intent.putExtra("itemObject",String.valueOf(jsonObject));
                        setResult(2001,intent);
                        finish();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                else {
                    curToast.setText("请选择采购物料并输入采购数量！");
                    curToast.show();
                }
                break;

            case R.id.btn_return:
                finish();
                break;
            default:
                break;
        }
    }

    private void ClearScreen() {
        edt_MaterialName.setText("");
        edt_Model.setText("");
        edt_DoorWidth.setText("");
        edt_BowlNo.setText("");
        edt_SquareWeight.setText("");
        edt_Number.setText("");
        edt_Uint.setText("");
        edt_NumberAssist.setText("");
        edt_UintAssist.setText("");
    }


    //直接连获取数据，与Handler配套使用;
    private void ReadSQLData(final String SQLComm) {
        System.out.println("SQLComm:"+SQLComm);
        Runnable run = new Runnable() {
            @Override
            public void run() {
                try{
                    JSONArray jsonArray  = DBUtil.QuerySQL2JSON(SQLComm);
                    System.out.println(jsonArray);
                    String OperType="";
                    //往界面转数据;
                    if ((jsonArray!=null)&&(jsonArray.length()>0)) {
                        if (jsonArray.optJSONObject(0).has("OperType")) OperType=jsonArray.optJSONObject(0).getString("OperType");
                        if (jsonArray.optJSONObject(0).has("OPERTYPE")) OperType=jsonArray.optJSONObject(0).getString("OPERTYPE");
                        if (OperType.equals("Detail")==true){
                            detailJSON=jsonArray;
                            //刷新界面;
                            Message msg = new Message();
                            msg.what = MSG_DetailOver;
                            fragmentHandler.removeMessages(msg.what);
                            fragmentHandler.sendMessage(msg);
                        }
                        if (OperType.equals("Material")==true){
                            searchJSON=jsonArray;
                            //刷新界面;
                            Message msg = new Message();
                            msg.what = MSG_Material;
                            fragmentHandler.removeMessages(msg.what);
                            fragmentHandler.sendMessage(msg);
                        }
                        if (jsonArray.optJSONObject(0).has("SQLExcept") == true) {
                            //刷新界面;
                            Message errmsg = new Message();
                            Bundle bundle=new Bundle();
                            bundle.putString("HintInfo",jsonArray.optJSONObject(0).getString("SQLMessage"));
                            errmsg.what = MSG_ERROR;
                            errmsg.setData(bundle);
                            fragmentHandler.removeMessages(errmsg.what);
                            fragmentHandler.sendMessage(errmsg);
                        }
                    } else {
                        //刷新界面;
                        Message errmsg = new Message();
                        Bundle bundle=new Bundle();
                        bundle.putString("HintInfo","未返回任何数据!");
                        errmsg.what = MSG_ERROR;
                        errmsg.setData(bundle);
                        fragmentHandler.removeMessages(errmsg.what);
                        fragmentHandler.sendMessage(errmsg);
                    }
                } catch (Exception e) {
                    //刷新界面;
                    Message errmsg = new Message();
                    Bundle bundle=new Bundle();
                    bundle.putString("HintInfo",e.getMessage());
                    errmsg.what = MSG_ERROR;
                    errmsg.setData(bundle);
                    fragmentHandler.removeMessages(errmsg.what);
                    fragmentHandler.sendMessage(errmsg);
                }
            }

        };
        new Thread(run).start();

    }

    // 隐藏手机键盘
    private void hideIM(View edt) {
        try {
            InputMethodManager im = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);
            IBinder windowToken = edt.getWindowToken();
            if (windowToken != null) {
                im.hideSoftInputFromWindow(windowToken, 0);
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
